App.controller("shopProductCtrl",function($scope, $rootScope, $location, $http, $compile, $stateParams, $timeout, path, publicService) {
	
	$scope.search = {};		//搜索参数
	var alertModel = {};	//alert模态框
	$scope.article = {};    //文章
	var limitParam2 = {imageSize:20, format:"jpg"};		//缩略图格式
	var compileId = null;    //如果是编辑的时候就有这个id
	var editor = UE.getEditor('editor');
	if(editor.container!=undefined){
		editor.destroy();
		editor = UE.getEditor('editor');
	}
	//表格对象
	$scope.gridOptions = {
			dataSource : {
				transport : {
					read : {
						url : path+"/server/querynews",
						type : 'POST',
						dataType : "json",
						contentType : "application/json"
					},
					parameterMap : function(options, operation) {
						if (operation == "read") {
							var parameter = {
								page : options.page, //当前页
								pageSize : options.pageSize,
								param:{
								}
							};
							console.log('查询参数',parameter);
							return kendo.stringify(parameter);
						}
					},
				},
				pageSize : 10,
				serverPaging : true,
				serverSorting: true,
				schema : {
					data : function(d) {
						return d.result.data;
					},
					total : function(d) {
						return d.result.total; //总条数
					}
				},
			},
			pageable : {  //分页
				pageSizes: [10, 20, 50, 100],  //每页显示记录数
			  },  
			sortable: {   //排序
				//allowUnsort: false,  //允许无序
			    mode: "multiple"     //排序模式：single，multiple
			  },
			editable: false,  //true：单个编辑；popup：弹出新页面编辑
			dataBound: function () {   //序号
		        var rows = this.items();
		        var page = this.pager.page() - 1;
		        var pagesize = this.pager.pageSize();
		        $(rows).each(function () {
		            var index = $(this).index() + 1 + page * pagesize;
		            var rowLabel = $(this).find(".row-number");
		            $(rowLabel).html(index);
		        });
		    },
			columns : [
					{    
						title : "序号",
						field : "id", 
						width : "50px"
					},
					{
						field : "article_title",         
						title : "文章标题",     
						width : "100px"
					},
					{
						field : "article_createTime",         
						title : "创建日期",     
						width : "100px"
					},
					{
						field : "likes_num",         
						title : "点赞量",     
						width : "100px"
					},
					{
						field : "collect_num",         
						title : "收藏量",     
						width : "100px"
					},
					{
						field : "evaluation_num",         
						title : "评价量",     
						width : "100px"
					},
					{
						field : "share_num",         
						title : "分享量",     
						width : "100px"
					},
					{
						field : "",         
						title : "操作",     
						width : "100px",
						template: function(e){
					    	var html = '';
					    	var	template_html ='<div class="operation k-state-default " ng-click="lookDetails(\''+e.article_id+'\')">编辑</div>'
					    					  +'<div class="operation k-state-default " ng-click="delectDetails(\''+e.article_id+'\')">删除</div>'
					    					  +'<div class="operation k-state-default " ng-click="copylink(\''+e.article_id+'\')">复制链接</div>'
					    					  
							return template_html;
					    	if(userInfo.user_type==1){  //开发者
					    		
					    	}else if(userInfo.user_type==2){  //企业
					    		
					    	}else if(userInfo.user_type==3){  //合伙人
					    		
					    	}else if(userInfo.user_type==4){  //品牌
					    		
					    	}else if(userInfo.user_type==5){  //专卖店
					    		
					    	}

					    	return html;
					    },
					}
			]
	};
	
	//查询
	$scope.search = function(){
		$scope.grid.dataSource.page(1);
	}
	//编辑(请求文章详情)
	$scope.lookDetails = function(id){
		compileId = id;      //赋值
		$http({
			   url:path+"/server/queryarticledetails/"+id,
			   method: 'GET'
		}).success(function(data){
			console.log(data); 
			if(data.code == '0'){
				var requestData = data.result;
				$scope.article.title = requestData.title;    //文章标题
				$scope.article.digest = requestData.digest;  //文章概述
				$scope.article.author = requestData.author;  //文章作者
				$('#publish_time2').val(requestData.create_time);  //发布时间
				publicService.showUploadImage("logo2_img","logo2_url",requestData.image);   //缩略图上传
				editor.setContent(requestData.details);   	   //内容
				
			}else{
				alert("请求数据失败!");
			}
			$scope.articleWindow.center().open();   //打开弹框 
		}).error(function(data){
			console.log(data);
	 
		})
	};
	//删除
	$scope.delectDetails = function(id){
		$http({
			   url:path+"/server/deleteArticleByArticleId/"+id,
			   method: 'GET'
		}).success(function(data){
			console.log(data);
			$scope.search();
		}).error(function(data){
			console.log(data);
		})
	};
	//全选，取消
	$scope.chooseAll = function(className,flag){
		publicService.chooseAllByClassName(className,flag);
	}
	
	//新建文章
	$scope.addArticle = function(){
		$scope.articleWindow.center().open();   //打开弹框 
	};
	//提交新增
	$scope.addNewArticle = function(){
		
				if(!$scope.article.title){
					alert("文章标题!");
					return;
				}
				if(!$scope.article.digest){
					alert("文章概述!");
					return;
				}
				if(!$scope.article.author){
					alert("文章作者!");
					return;
				}
				if(!$('#publish_time2').val()){
					alert("发布日期!");
					return;
				}
				if(!$('#logo2_url').val()){
					alert("缩略图暂未上传!");
					return;
				}
				if(!editor.getContent()){
					alert("文章内容!");
					return;
				}
				$scope.article.title = $scope.article.title;        //标题
				$scope.article.digest =$scope.article.digest;    //概述
				$scope.article.author =$scope.article.author;   //作者
				$scope.article.publish_time = $('#publish_time2').val();   //发布时间
				$scope.article.image = $('#logo2_url').val();   //缩略图
				$scope.article.details = editor.getContent();    //文章段落
				var urls = '';
				if(compileId){
					urls = path+"/server/updateArticle";   //编辑
					$scope.article.id = compileId;
				}else{
					urls = path+"/server/addArticle";    //新增
				}
				$http({
					   url:urls,
					   method: 'POST',
					   data:angular.toJson($scope.article)
				}).success(function(data){
					console.log(data);
					if(data.code =='0'){
						alert("提交成功！"); 
						$scope.articleWindow.center().close();   //打开弹框 
						window.location.reload();   //页面刷新
					}else{
						alert('接口异常！');
					}

				}).error(function(data){
					console.log(data);
				})

	};
	publicService.initDate("publish_time2");       
	publicService.uploadImage("logo2","logo2_img","logo2_url",limitParam2);		//缩略图
	
	

	
	
	
	
	
});